package com.changlie.generator.test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.changlie.generator.dao.SqlServerDao;
import com.changlie.generator.index.GenerateBean;
@SuppressWarnings("all")
public class Demo1 {

	public static void main(String[] args) throws Exception {
		
		new GenerateBean().start();
		
//		TableFields("sys_role", "af");
		TableFields("sys_role");
	}

	private static void TableFields(String tableName) {
		TableFields(tableName, null);
	}

	private static void TableFields(String tableName, String prefix) {
		
		SqlServerDao dao = new SqlServerDao();
		List<String> fields = dao.getFields(tableName);
		StringBuilder sss = new StringBuilder();
		fields.forEach(s -> {
//			sss.append("{field:'").append(s).append("',title:'',  editor:'text'},\n");
			if(prefix==null) 
				sss.append(s).append(", ");
			else
				sss.append(prefix+".").append(s).append(", ");
		});
		System.out.println(sss);
		System.out.println(fields);
	}
	
	
	public static void test() throws Exception  {
		File f = new File("d:/d.html");
		BufferedReader r = new BufferedReader(new FileReader(f));
		String s = "";
//		boolean isTrOver = true;
		List<Map<String, Object>> list = new ArrayList<>();
		Map<String, Object> map = null;
		int colIndex = 0;
		while((s = r.readLine())!=null){
//			System.out.println(s);
			s = s.trim();
			if("".equals(s) || "</td>".equals(s)) continue;
			
			if("<tr>".equals(s)){
//				isTrOver = false;
				 map = new HashMap<>();
				 continue;
			}
			if("<td>".equals(s)){
				colIndex ++;
				continue;
			}
			if("</tr>".equals(s)){
				list.add(map);
				 colIndex = 0;
				 map = null;
				 continue;
			}
			
			if(s.startsWith("<p>")){
				s = s.substring(3, s.indexOf("</")).trim();
				switch (colIndex) {
				case 1:
					if(map.containsKey("sql")){
						map.put("sql1", s);
					}else if(map.containsKey("sql1")){
						map.put("sql2", s);
					}else if(map.containsKey("sql2")){
						map.put("sql3", s);
					}else{
						map.put("sql", s);
					}
					break;
				case 2:
					map.put("jdbc", s);
					break;
				case 3:
					map.put("java", s);
					break;
				}
			}
		}
		r.close();
		
		BufferedWriter bw = new BufferedWriter(new FileWriter("d:/demo.properties"));
		for(Map<String, Object> m : list){
//			System.out.println(m);
			String value = (String) m.get("java");
			if(value.contains("BigDecimal")){
				value = "Double";
			}
			if(value.contains("Timestamp") || value.contains("Time") || value.contains("Date")){
				value = "Date";
			}
			String First = value.substring(0, 1);
			value = First.toUpperCase()+ value.substring(1);
			if(m.containsKey("sql")){
				String key = (String) m.get("sql");
				bw.write("sqlserver."+key+"="+value);
				bw.newLine();
			}
			if(m.containsKey("sql1")){
				String key = (String) m.get("sql1");
				bw.write("sqlserver."+key+"="+value);
				bw.newLine();
			}
			if(m.containsKey("sql2")){
				String key = (String) m.get("sql2");
				bw.write("sqlserver."+key+"="+value);
				bw.newLine();
			}
			
		}
		bw.flush();
		bw.close();
	}
}
